package com.rdgk.newtech.controller;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.service.NewTechAnalyseService;
import com.rdgk.newtech.service.ProjectService;
import com.rdgk.newtech.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: 高亚杰
 * @description: 技术动态数据接口Controller
 */
@RestController
@RequestMapping("/newtechanalyse")
public class NewTechAnalyseController {

	@Autowired
	private NewTechAnalyseService newTechAnalyseService;

	/**
	 * 最新技术动态分析方法
	 */
	@PostMapping("/latestTechnologyTrends")
	public JSONObject latestTechnologyTrends(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		return CommonUtil.successJson(newTechAnalyseService.latestTechnologyTrends(Integer.parseInt(topicId),null));
	}

	/**
	 * 技术发展趋势
	 * @return
	 */
    @PostMapping("/technologyDevelopmentTrends")
    public JSONObject technologyDevelopmentTrends(HttpServletRequest request) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String topicId = request.getParameter("topicId");
		Date startDate = null;
		Date endDate = null;
		try{
			startDate = sf.parse(request.getParameter("startDate"));
			endDate = sf.parse(request.getParameter("endDate"));
			System.out.println(startDate+"++++++"+endDate);
		}catch (Exception e){
			e.printStackTrace();
		}
		String dateType = request.getParameter("dateType");
		String category = request.getParameter("category");
		if(StringUtils.isEmpty(dateType)){
        	return CommonUtil.successJson(newTechAnalyseService.technologyDevelopmentTrends(Integer.parseInt(topicId),null,startDate,endDate));

		}else{
        	return CommonUtil.successJson(newTechAnalyseService.technologyDevelopmentTrends(Integer.parseInt(topicId),null,startDate,endDate,dateType,category));
		}
    }

	/**
	 *技术发展历程
	 */
	@PostMapping("/technologyDevelopmentCours")
	public JSONObject technologyDevelopmentCours(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		String category = request.getParameter("category");
		Map<String,Object> results = new HashMap<String,Object>();
		List<Map<String,Object>> courses = newTechAnalyseService.technologyDevelopmentCours(Integer.parseInt(topicId),category);
		results.put("courses",courses);
		List<Long> years = new ArrayList<Long>();
		for (Map<String,Object> course:courses
			 ) {
			Long year = null;
			if(course.get("publishyear") instanceof Long)
				year = (Long)course.get("publishyear");
			else
				year = Long.parseLong(((Integer)course.get("publishyear")).intValue()+"");
			if(!years.contains(year)){
				years.add(year);
			}
		}
		Collections.sort(years);
		results.put("years",years);
		return CommonUtil.successJson(results);
	}

	/**
	 *技术热点
	 */
	@PostMapping("/technologyHotspot")
	public JSONObject technologyHotspot(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		return CommonUtil.successJson(newTechAnalyseService.technologyHotspot(Integer.parseInt(topicId),null));
	}

	/**
	 *技术动态信息区域分布
	 */
	@PostMapping("/technologyRegionalDistribution")
	public JSONObject technologyRegionalDistribution(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		return CommonUtil.successJson(newTechAnalyseService.technologyRegionalDistribution(Integer.parseInt(topicId),null));
	}

	/**
	 *技术动态信息来源分布
	 */
	@PostMapping("/technologySourceDistribution")
	public JSONObject technologySourceDistribution(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		String category = request.getParameter("category");
		if(StringUtils.isEmpty(category)){
			return CommonUtil.successJson(newTechAnalyseService.technologySourceDistribution(Integer.parseInt(topicId),null));

		}else{
			if("news".equals(category.toLowerCase()))
				return CommonUtil.successJson(newTechAnalyseService.technologySourceDistribution_news(Integer.parseInt(topicId),null));
			if("paper".equals(category.toLowerCase()))
				return CommonUtil.successJson(newTechAnalyseService.technologySourceDistribution_paper(Integer.parseInt(topicId),null));
			if("patent".equals(category.toLowerCase()))
				return CommonUtil.successJson(newTechAnalyseService.technologySourceDistribution_patent(Integer.parseInt(topicId),null));
		}
		return CommonUtil.successJson();
	}

	/**
	 *热词图谱
	 */
	@PostMapping("/hotWordsAtlas")
	public JSONObject hotWordsAtlas(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		String keyword = request.getParameter("keyword");
		String category = request.getParameter("category");
		if(StringUtils.isEmpty(category)){
			return CommonUtil.successJson(newTechAnalyseService.hotWordsAtlas(Integer.parseInt(topicId),keyword));

		}else {
			return CommonUtil.successJson(newTechAnalyseService.hotWordsAtlas(Integer.parseInt(topicId),keyword,category));
//			switch (category){
//				case "News":
//				case "news":
//					return CommonUtil.successJson(newTechAnalyseService.hotWordsAtlasNews(Integer.parseInt(topicId),keyword));
//				case "Paper":
//				case "paper":
//					return CommonUtil.successJson(newTechAnalyseService.hotWordsAtlasPaper(Integer.parseInt(topicId),keyword));
//				case "Patent":
//				case "patent":
//					return CommonUtil.successJson(newTechAnalyseService.hotWordsAtlasPatent(Integer.parseInt(topicId),keyword));
//				default:
//					return CommonUtil.successJson();
//			}

		}
	}
	/**
	 *热词发展趋势
	 */
	@PostMapping("/hotWordDevelopmentTrend")
	public JSONObject hotWordDevelopmentTrend(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		String topnumber = request.getParameter("topnumber");
		String category = request.getParameter("category");
		Date startDate = null;
		Date endDate = null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		try{
			startDate = sf.parse(request.getParameter("startDate"));
			endDate = sf.parse(request.getParameter("endDate"));
		}catch (Exception e){
			e.printStackTrace();
		}
		if(StringUtils.isEmpty(category)){
			return CommonUtil.successJson(newTechAnalyseService.hotWordDevelopmentTrend(Integer.parseInt(topicId),Integer.parseInt(topnumber==null?"5":topnumber),null,startDate,endDate));

		}else{
			return CommonUtil.successJson(newTechAnalyseService.hotWordDevelopmentTrend(Integer.parseInt(topicId),Integer.parseInt(topnumber==null?"5":topnumber),null,startDate,endDate,category.toLowerCase()));

		}
	}

	/**
	 * 共词分析
	 */
    @PostMapping("/coWordAnalysis")
    public JSONObject coWordAnalysis(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		String category = request.getParameter("category");
		if(StringUtils.isEmpty(category)){
			return CommonUtil.successJson(newTechAnalyseService.coWordAnalysis(Integer.parseInt(topicId)));

		}else {
			return CommonUtil.successJson(newTechAnalyseService.coWordAnalysis(Integer.parseInt(topicId),category));

		}
    }
	/**
	 * 共词分析
	 */
	@PostMapping("/hotWordDetail")
	public JSONObject hotWordDetail(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		String wordId = request.getParameter("wordId");
		return CommonUtil.successJson(newTechAnalyseService.hotWordDetail(Integer.parseInt(topicId),Integer.parseInt(wordId)));
	}
	/**
	 * 论文详情
	 */
	@PostMapping("/paperDetail")
	public JSONObject paperDetail(HttpServletRequest request) {
		String paperId = request.getParameter("paperId");
		return CommonUtil.successJson(newTechAnalyseService.paperDetail(Integer.parseInt(paperId)));
	}
	/**
	 * 专利详情
	 */
	@PostMapping("/patentDetail")
	public JSONObject patentDetail(HttpServletRequest request) {
		String patentId = request.getParameter("patentId");
		return CommonUtil.successJson(newTechAnalyseService.patentDetail(Integer.parseInt(patentId)));
	}
	/**
	 * 论文详情
	 */
	@PostMapping("/authorDetail")
	public JSONObject authorDetail(HttpServletRequest request) {
		String authorId = request.getParameter("authorId");
		return CommonUtil.successJson(newTechAnalyseService.authorDetail(Integer.parseInt(authorId)));
	}
	/**
	 * 专利详情
	 */
	@PostMapping("/orgDetail")
	public JSONObject orgDetail(HttpServletRequest request) {
		String orgId = request.getParameter("orgId");
		return CommonUtil.successJson(newTechAnalyseService.orgDetail(Integer.parseInt(orgId)));
	}
	/**
	 * 热词云图
	 */
	@PostMapping("/getHotwords")
	public JSONObject getHotwords(HttpServletRequest request) {
		String topicId = request.getParameter("topicId");
		String category = request.getParameter("category");
		if("news".equals(category))
			category = "0";
		if("paper".equals(category))
			category = "1";
		if("patent".equals(category))
			category = "2";
		return CommonUtil.successJson(newTechAnalyseService.getHotwordByType(Integer.parseInt(topicId),category));
	}
}
